/*
 * @Author: wuqingchun
 * @Date: 2024-07-29 16:59:31
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-07-31 16:41:28
 */
#include <stdio.h>
#include <stdarg.h>


#include "ev_log.h"

EV_LOG_CB_PF g_pfLogCallback = NULL;

const char* g_apcLogLevelStr[LOG_LEVEL_ERROR + 1] = 
{
    "DEBUG", 
    "INFO ", 
    "WARN ", 
    "ERROR"
};

static void __log_Output(int iLogLevel, const char* pcMsg)
{
    if (g_pfLogCallback != NULL)
    {
        g_pfLogCallback(iLogLevel, pcMsg);
    }
    else
    {
        const char *pcLevelStr = "???";
        if (iLogLevel <= LOG_LEVEL_ERROR)
        {
            pcLevelStr = g_apcLogLevelStr[iLogLevel];
        }

        (void)fprintf(stderr, "[%s] %s\n", pcLevelStr, pcMsg);
    }
}

void EV_log_SetCB(EV_LOG_CB_PF pfCB)
{
    g_pfLogCallback = pfCB;
}

void EV_log_Output(int iLogLevel, const char *pcFmt, ...)
{
    char acBuf[1024];
	int iLen = 0;

    va_list ap;

	va_start(ap, pcFmt);

    if (pcFmt != NULL)
    {
        iLen = vsnprintf(acBuf, sizeof(acBuf), pcFmt, ap);
    }
    else
    {
        acBuf[0] = '\0';
    }
    va_end(ap);

    __log_Output(iLogLevel, acBuf);
}